package render

import (
	"gin-sns/app/common/avatar"
	"gin-sns/app/model"
	categoryModel "gin-sns/app/model/module/category"
	userModel "gin-sns/app/model/module/user"
)

func BuildUser(user *userModel.Entity) *model.UserInfo {
	if user == nil {
		return nil
	}
	a := user.Avatar
	if len(a) == 0 {
		a = avatar.GetDefaultAvatar(user.Id)
	}

	return &model.UserInfo{
		//Id:          user.Id,
		Username:    user.UserName,
		Nickname:    user.Nickname,
		Avatar:      a,
		Email:       user.UserEmail,
		Type:        user.Type,
		Description: user.Description,
		PasswordSet: len(user.Password) > 0,
		CreateTime:  user.CreateTime.Unix(),
	}
}

func BuildUsers(users []userModel.Entity) []model.UserInfo {
	if len(users) == 0 {
		return nil
	}
	var responses []model.UserInfo
	for _, user := range users {
		item := BuildUser(&user)
		if item != nil {
			responses = append(responses, *item)
		}
	}
	return responses
}

func BuildCategory(category *categoryModel.Entity) *model.CategoryResponse {
	if category == nil {
		return nil
	}
	return &model.CategoryResponse{CategoryId: category.Id, CategoryName: category.Title}
}

func BuildCategories(categories []categoryModel.Entity) []model.CategoryResponse {
	if categories == nil || len(categories) == 0 {
		return nil
	}
	var responses []model.CategoryResponse
	for _, category := range categories {
		responses = append(responses, *BuildCategory(&category))
	}
	return responses
}
